<?php

namespace api\exception;

use think\App;
use think\Config;
use think\exception\Handle;

class JsonException extends Handle
{
    public function render(\Exception $exception)
    {
        if (App::$debug) {
            // 调试模式，获取详细的错误信息
            $data = [
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
                'message' => $this->getMessage($exception)
            ];
        } else {
            // 部署模式仅显示 Code 和 Message
            $data = [
                'code' => $this->getCode($exception),
                'message' => $this->getMessage($exception),
            ];

            if (!Config::get('show_error_msg')) {
                // 不显示详细错误信息
                $data['message'] = Config::get('error_message');
            }
        }

        $result = [
            'code' => 400,
            'msg' => $data['message'],
            'time' => $_SERVER['REQUEST_TIME'],
            'data' => $data,
        ];
        return json($result);
    }
}